﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    /// <summary>
    /// 用户表
    /// </summary>
    public class BLL_user
    {
        /// <summary>
        /// 增
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        public bool insert(_user us)
        {
            if (us._user_email != null)
            {
                GodHelperDBDataContext da = new GodHelperDBDataContext();
                da._user.InsertOnSubmit(us);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        public bool delete(_user us)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da._user.Where(item => item._user_email == us._user_email);
            if (p.Any())
            {
                var _trade = da.trade.Where(item => item.trade_refrences_user_email == us._user_email);
                if (_trade.Any())
                {
                    for (int i = 0; i < _trade.ToList().Count; i++)
                    {
                        var tradeid = _trade.ToList()[i];
                        da.trade.DeleteOnSubmit(tradeid);
                    }
                }

                var _product = da.product.Where(item => item.product_refrences_user_email == us._user_email);
                if (_product.Any())
                {
                    for (int i = 0; i < _product.ToList().Count; i++)
                    {
                        var productid = _product.ToList()[i];
                        da.product.DeleteOnSubmit(productid);
                    }
                }

                var _task = da.task.Where(item => item.task_refrences_user_email == us._user_email);
                if (_task.Any())
                {
                    for (int i = 0; i < _task.ToList().Count; i++)
                    {
                        var taskid = _task.ToList()[i];
                        da.task.DeleteOnSubmit(taskid);
                    }
                }

                var _contact = da.contact.Where(item => item.contact_refrences_user_email == us._user_email);
                if (_contact.Any())
                {
                    for (int i = 0; i < _contact.ToList().Count; i++)
                    {
                        var contactid = _contact.ToList()[i];
                        da.contact.DeleteOnSubmit(contactid);
                    }
                }

                var info = da._userinfo.Where(item => item._user_email == us._user_email);
                if (info.Any())
                {
                    for (int i = 0; i < info.ToList().Count; i++)
                    {
                        var infoid = info.ToList()[i];
                        da._userinfo.DeleteOnSubmit(infoid);
                    }
                }

                var user = p.ToList()[0];
                da._user.DeleteOnSubmit(user);
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 改
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        public bool update(_user us)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            var p = da._user.Where(item => item._user_email == us._user_email);
            if (p.Any())
            {
                var user = p.ToList()[0];
                user._user_psw = us._user_psw;
                da.SubmitChanges();
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查
        /// </summary>
        /// <param name="ui"></param>
        /// <returns></returns>
        public _user search(_user us)
        {
            GodHelperDBDataContext da = new GodHelperDBDataContext();
            if (us._user_email != null)
            {
                var p = da._user.Where(item => item._user_email == us._user_email);
                if (p.Any())
                {
                    return p.ToList()[0];
                }
            }
            return null;
        }
    }
}
